<?php

namespace app\admin\controller\mall;

use app\admin\traits\Curd;
use app\common\controller\AdminController;
use app\common\model\MallOrder;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use EasyAdmin\tool\CommonTool;
use jianyan\excel\Excel;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="销售统计")
 */
class Seller extends AdminController
{
    use Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model =(new MallOrder());
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            // 总收入金额
            $totalpayprice =0;
            // 总成本金额
            $totalcostprice =0;
            // 退款金额
            $refundprice =0;
            foreach ($list as $k=>$v){
                if(in_array($v['status'],[1,2])){
                    $totalpayprice+=$v['payprice'];
                    $totalcostprice+=$v['cost_price']*$v['goods_num'];
                }
                if($v['status'] == 3){
                    $refundprice+=$v['payprice'];
                }
            }
            $totalpayprice =round($totalpayprice,2);
            $totallirun =round($totalpayprice-$totalcostprice,2);
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
                'totalprice' =>$totalpayprice,
                'totallirun' =>$totallirun,
                'totalcostprice'=>$totalcostprice,
                'refundprice' =>$refundprice
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="导出")
     */
    public function export()
    {
        $where=[];
        $ids =$this->request->param('id','','trim');
        $where[] =[['id','in',$ids]];
        $tableName = $this->model->getName();
        $tableName = CommonTool::humpToLine(lcfirst($tableName));
        $prefix = config('database.connections.mysql.prefix');
        $dbList = Db::query("show full columns from {$prefix}{$tableName}");
        $header = [];
        foreach ($dbList as $vo) {
            $comment = !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'];
            if (!in_array($vo['Field'], ['openid','specs_value_ids','short_title','orderno','validity_time','hxuser_id','pay_time','hx_time','canceltime','refund_time','discount','remark','manager_id','goods_id','market_price','tags','goods_logo','goods_price','isread','add_date','realname','mobile','orderprice','pay_type','bonus_id','bonus_price','doors','door_id','update_time','delete_time'])) {
                $header[] = [$comment, $vo['Field']];
            }
        }
        $list = $this->model
            ->where($where)
            ->order('id', 'desc')
            ->select()
            ->toArray();
        foreach ($list as $k=>$v){
            switch ($v['status']){
                case 0:
                    $list[$k]['status'] ='待支付';
                    break;
                case 1:
                    $list[$k]['status'] ='待核销';
                    break;
                case 2:
                    $list[$k]['status'] ='已核销';
                    break;
                case 3:
                    $list[$k]['status'] ='已退款';
                    break;
                case 4:
                    $list[$k]['status'] ='订单取消';
                    break;
                default:
                    break;
            }
        }
        $fileName = time();
        return Excel::exportData($list, $header, $fileName, 'xlsx');
    }
}